//
//  SISSPassengerResourceEnrollViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPassengerResourceEnrollViewController.h"
#import "SISSPickerView.h"
#import "SISSTextView.h"
#import "SISSPassengerResourceModel.h"
#import "SISSEnrollPassengerDetailViewController.h"
#import "SISSCooperationDetailViewController.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"
#import "SISSMyEnrollPassengerViewController.h"
#import "SISSPassengerResourceCooperationViewController.h"
#import "SISSNetworkingManager+Home.h"

#import "SISSHouseResourceTagView.h"
#import "SISSPassengerRegistSuccessViewController.h"

@interface SISSPassengerResourceEnrollViewController ()<UITextFieldDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *intentionRegionTextField;
@property (weak, nonatomic) IBOutlet UITextField *intentionCommunityTextField;
@property (weak, nonatomic) IBOutlet UITextField *houseTypeTextField;
@property (weak, nonatomic) IBOutlet UITextField *minBudgetTextField;
@property (weak, nonatomic) IBOutlet UITextField *maxBudgetTextField;
@property (weak, nonatomic) IBOutlet UITextField *minAreaTextField;
@property (weak, nonatomic) IBOutlet UITextField *maxAreaTextField;
@property (weak, nonatomic) IBOutlet UITextField *minFloorTextField;
@property (weak, nonatomic) IBOutlet UITextField *maxFloorTextField;
@property (weak, nonatomic) IBOutlet UITextField *decorationTextField;
@property (weak, nonatomic) IBOutlet UITextField *directionTextField;
@property (weak, nonatomic) IBOutlet UITextField *purchaseTextField;
@property (weak, nonatomic) IBOutlet UILabel *specialDemandTagsLabel;
@property (weak, nonatomic) IBOutlet UITextView *otherDescriptionTextView;
@property (weak, nonatomic) IBOutlet UITextField *buyerNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *buyerIDCardNoTextField;
@property (weak, nonatomic) IBOutlet UITextField *buyerPhoneNumTextField;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

@property (weak, nonatomic) IBOutlet UITextField *allowAgentCoopTextField;
@property (weak, nonatomic) IBOutlet UITextField *releaseLimitTextField;
@property (weak, nonatomic) IBOutlet UITextField *ownerComissonPercentTextField;
@property (weak, nonatomic) IBOutlet UILabel *partnerCommissionPercentLabel;
@property (weak, nonatomic) IBOutlet UITextView *cooperationDescriptionTextView;
@property (weak, nonatomic) IBOutlet UIView *allowAgentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *allowAgentHeightContraint;

@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewConstraint;

@property (assign, nonatomic) NSInteger allowAgent; // 1.
/** 房源合作范围 */
@property (assign, nonatomic) NSInteger cooperation_range; // 1.全行业2.公司。3门店

/** commissionPercentIndex */
@property (assign, nonatomic) NSInteger commissionPercentIndex;
/** 佣金比例 */
@property (assign, nonatomic) NSInteger commissionRate;

/** areaFirstIndex */
@property (assign, nonatomic) NSInteger areaFirstIndex;
/** areaSecondIndex */
@property (assign, nonatomic) NSInteger areaSecondIndex;
/** houseTypeIndex */
@property (assign, nonatomic) NSInteger houseTypeIndex;
/** directionIndex */
@property (assign, nonatomic) NSInteger directionIndex;
/** decorationIndex */
@property (assign, nonatomic) NSInteger decorationIndex;
/** purchaseIndex */
@property (assign, nonatomic) NSInteger purchaseIndex;

@property (strong, nonatomic) NSMutableArray *tagIndexArray;

/** 记录是否点击了确定 */
@property (assign, nonatomic) BOOL isConfirm;

@end

@implementation SISSPassengerResourceEnrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isConfirm = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.intentionCommunityTextField.delegate = self;
    self.minBudgetTextField.delegate = self;
    self.maxBudgetTextField.delegate = self;
    self.minAreaTextField.delegate = self;
    self.maxAreaTextField.delegate = self;
    self.minFloorTextField.delegate = self;
    self.maxFloorTextField.delegate = self;
    self.buyerNameTextField.delegate = self;
    self.buyerIDCardNoTextField.delegate = self;
    self.buyerPhoneNumTextField.delegate = self;
    self.otherDescriptionTextView.delegate = self;
    
//    self.cooperationDescriptionTextView.placeholder = @"请输入合作说明";
    self.cooperationDescriptionTextView.layer.borderWidth = 1;
    self.cooperationDescriptionTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    // same font
    self.cooperationDescriptionTextView.font = [UIFont systemFontOfSize:15.f];
    
    self.commissionPercentIndex = 50;
    self.commissionRate = 50;
    self.allowAgent = 2;
    self.bottomView.hidden = YES;
    self.bottomViewConstraint.constant = 1;
    
    if (self.passengerModel) {
        
        self.allowAgentView.hidden = YES;
        self.allowAgentHeightContraint.constant = 1;
        
        self.intentionRegionTextField.text = [NSString stringWithFormat:@"%@ %@",self.passengerModel.region,self.passengerModel.regionDetail];
        self.intentionCommunityTextField.text = self.passengerModel.neighbourhood;
        self.houseTypeTextField.text = self.passengerModel.houseType;
        self.minBudgetTextField.text = [NSString stringWithFormat:@"%0.f",self.passengerModel.minPrice];
        self.maxBudgetTextField.text = [NSString stringWithFormat:@"%0.f",self.passengerModel.maxPrice];
        self.minAreaTextField.text = [NSString stringWithFormat:@"%0.f",self.passengerModel.minArea];
        self.maxAreaTextField.text = [NSString stringWithFormat:@"%0.f",self.passengerModel.maxArea];
        self.minFloorTextField.text = [NSString stringWithFormat:@"%zd",self.passengerModel.minFloor];
        self.maxFloorTextField.text = [NSString stringWithFormat:@"%zd",self.passengerModel.maxFloor];
        self.decorationTextField.text = self.passengerModel.decoration;
        self.directionTextField.text = self.passengerModel.direction;
        self.purchaseTextField.text = self.passengerModel.purpose;
        self.otherDescriptionTextView.text = self.passengerModel.otherdesc;
        self.buyerNameTextField.text = self.passengerModel.name;
        self.buyerIDCardNoTextField.text = self.passengerModel.idNumber;
        self.buyerPhoneNumTextField.text = self.passengerModel.phoneNumber;
        
        [self.confirmButton setTitle:@"保存" forState:UIControlStateNormal];
        
        self.buyerNameTextField.userInteractionEnabled = NO;
        self.buyerIDCardNoTextField.userInteractionEnabled = NO;
        self.buyerPhoneNumTextField.userInteractionEnabled = NO;
    }
    //添加事件
    [self.intentionCommunityTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}


//实现方法
- (void)textFieldDidChange:(UITextField *)textField {
    if (textField.text.length > 10) {
        //提示语
        [SISSTools showSVTitle:@"小区不能超过10个字" andTime:1 andColor:YES];
        //截取
        textField.text = [textField.text substringToIndex:10];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
//    if (textField == self.intentionCommunityTextField) { // 意向小区
//        //在输入过程中 判断加上输入的字符 是否超过限定字数
//        NSString *str = [NSString stringWithFormat:@"%@", textField.text];
//        NSString *regex = @"[\u4e00-\u9fa5]+";
//        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
//
//        if ([pred evaluateWithObject:textField.text] &&str.length > 10) {
//            [SISSTools showSVTitle:@"小区最多10个字" andTime:1 andColor:YES];
//            textField.text = [textField.text substringToIndex:10];
//            return NO;
//        }
//    }
    
    // 预算
    if (textField == self.minBudgetTextField || textField == self.maxBudgetTextField) {
        //在输入过程中 判断加上输入的字符 是否超过限定字数
        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (str.length > 9) {
            [SISSTools showSVTitle:@"预算不能超过9个数字" andTime:1 andColor:YES];
            
            return NO;
        }
    }
    // 面积
    if (textField == self.minAreaTextField || textField == self.maxAreaTextField) {
        //在输入过程中 判断加上输入的字符 是否超过限定字数
        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (str.length > 9) {
            [SISSTools showSVTitle:@"面积不能超过9个数字" andTime:1 andColor:YES];
            
            return NO;
        }
    }
    
    // 楼层区间
    if (textField == self.minFloorTextField || textField == self.maxFloorTextField) {
        //在输入过程中 判断加上输入的字符 是否超过限定字数
        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (str.length > 3) {
            [SISSTools showSVTitle:@"楼层不能超过3位数字" andTime:1 andColor:YES];
            return NO;
        }
    }
    
    
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    // 预算
    if (self.minBudgetTextField.text.length > 0 && textField == self.maxBudgetTextField) {

        if ([self.minBudgetTextField.text floatValue] > [textField.text floatValue]) {
            [SISSTools showSVTitle:@"最高预算不能小于最低预算字" andTime:2 andColor:YES];
        }
    }
    
    // 面积
    if (self.minAreaTextField.text.length > 0 && textField == self.maxAreaTextField) {
    
        if ([self.minAreaTextField.text floatValue] > [textField.text floatValue]) {
            [SISSTools showSVTitle:@"最大面积不能小于最小面积" andTime:2 andColor:YES];
        }
    }
    // 楼层
    if (self.minFloorTextField.text.length > 0 && textField == self.maxFloorTextField) {
        
        if ([self.minFloorTextField.text floatValue] > [textField.text floatValue]) {
            [SISSTools showSVTitle:@"最高楼层不能小于最低楼层" andTime:2 andColor:YES];
        }
    }
    
    // 名字
    if (textField == self.buyerNameTextField && textField.text.length < 2) {
        
        [SISSTools showSVTitle:@"姓名不能少于2个字" andTime:1 andColor:YES];
    }
}


#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    //在输入过程中 判断加上输入的字符 是否超过限定字数
    NSString *str = [NSString stringWithFormat:@"%@%@",textView.text,text];
    if (str.length > 500) {
        [SISSTools showSVTitle:@"需求说明不能超过500个字" andTime:1 andColor:YES];
        return NO;
    }

    return YES;
}


- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/**
 返回关闭按钮的点击事件
 */
- (void)back {
    
    [self.view endEditing:YES];

    if (self.isConfirm) { // 判断是否点击了确定
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        
        NSString *title = @"放弃";
        NSString *message = self.passengerModel ? @"修改还未完成，您确定放弃吗？" : @"登记还未完成，您确定放弃吗？";
        
        [self alertConfirmCacellActionAlertWithTitle:title message:message];
    }
  
}



/**
 意向区域选择点击事件
 */
- (IBAction)intentionRegionChooseTapAction:(id)sender {
    
    [self.view endEditing:YES];
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
    pickerView.firstContainUnlimited = YES;
    pickerView.title = @"区域";
    pickerView.isArea = YES;
    pickerView.firstIndex = self.areaFirstIndex;
    pickerView.secondIndex = self.areaSecondIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        weakself.intentionRegionTextField.textColor = COLOR(0x334A60);
        weakself.intentionRegionTextField.font = [UIFont boldSystemFontOfSize:13];
        weakself.intentionRegionTextField.text = [NSString stringWithFormat:@"%@ %@",firstTitle,secondTitle];
        weakself.areaFirstIndex = firstIndex;
        weakself.areaSecondIndex = secondIndex;
    };
}

/**
 户型选择点击事件
 */

- (IBAction)houseTypeChooseTapAction:(id)sender {
    
    [self.view endEditing:YES];
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
    pickerView.title = @"户型";
    pickerView.titleArray = @[@"一房", @"二房", @"三房", @"四房", @"四房以上", @"复式", @"别墅", @"其他"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.houseTypeIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        weakself.houseTypeTextField.textColor = COLOR(0x334A60);
        weakself.houseTypeTextField.font = [UIFont boldSystemFontOfSize:13];
        weakself.houseTypeTextField.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.houseTypeIndex = firstIndex;
    };
}

/**
 装修程度选择点击事件
 */
- (IBAction)decorationChooseTapAction:(id)sender {
    
    [self.view endEditing:YES];
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
    pickerView.title = @"装修程度";
    pickerView.titleArray = @[@"不限", @"毛坯", @"普装", @"精装", @"豪装"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.decorationIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        weakself.decorationTextField.textColor = COLOR(0x334A60);
        weakself.decorationTextField.font = [UIFont boldSystemFontOfSize:13];
        weakself.decorationTextField.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.decorationIndex = firstIndex;
        [weakself directionChooseTapAction:nil];
    };
}

/**
 朝向选择点击事件
 */
- (IBAction)directionChooseTapAction:(id)sender {
    
    [self.view endEditing:YES];
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
    pickerView.title = @"朝向";
    pickerView.titleArray = @[@"不限", @"朝东", @"朝南", @"朝西", @"朝北", @"东南", @"东北", @"西北", @"西南"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.directionIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        weakself.directionTextField.textColor = COLOR(0x334A60);
        weakself.directionTextField.font = [UIFont boldSystemFontOfSize:13];
        weakself.directionTextField.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.directionIndex = firstIndex;
        [weakself purchasePurposeChooseTapAction:nil];
        
    };
}

/**
 购房目的选择点击事件
 */
- (IBAction)purchasePurposeChooseTapAction:(id)sender {
    
    [self.view endEditing:YES];
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
    pickerView.title = @"购房目的";
    pickerView.titleArray = @[@"不限", @"换房", @"就业", @"求学", @"结婚", @"养老", @"投资"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.purchaseIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        weakself.purchaseTextField.textColor = COLOR(0x334A60);
        weakself.purchaseTextField.font = [UIFont boldSystemFontOfSize:13];
        weakself.purchaseTextField.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.purchaseIndex = firstIndex;
        
    };
}

/**
 房源标签选择点击事件
 */
- (IBAction)specialDemandsTagChooseTapAction:(id)sender {
    
    [self.view endEditing:YES];
    
    SISSHouseResourceTagView *pickerView = [[SISSHouseResourceTagView alloc] initWithFrame:self.view.bounds isDemand:YES];

    pickerView.cacheIndexArray = self.tagIndexArray;
    __weak typeof(self) weakself = self;
    pickerView.getStringArrBlock = ^(NSMutableArray *stringArray, NSMutableArray *indexArray) {

        NSMutableString *mStr = [NSMutableString string];
        for (int i = 0; i < stringArray.count; i++) {
            [mStr appendFormat:@"%@ ",stringArray[i]];
        }
        weakself.specialDemandTagsLabel.textColor = COLOR(0x334A60);
        weakself.specialDemandTagsLabel.font = [UIFont boldSystemFontOfSize:13];
        weakself.specialDemandTagsLabel.text = mStr.copy;

        weakself.tagIndexArray = indexArray;
    };
}

- (IBAction)allowAgentCoopTapAction:(id)sender {
    
    [self.view endEditing:YES];
    if (self.passengerModel) return;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    alertController.view.tintColor = [UIColor blackColor];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alertController addAction:cancelAction];
    
    NSArray *limitArr = @[@"允许", @"不允许"];
    __weak typeof(self) weakSelf = self;
    for (NSInteger i = 0; i < limitArr.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:limitArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.allowAgent = i + 1;
            weakSelf.allowAgentCoopTextField.text = limitArr[i];
            weakSelf.allowAgentCoopTextField.font = [UIFont systemFontOfSize:16];
            if (i == 0) {
                self.bottomView.hidden = NO;
                self.bottomViewConstraint.constant = 434;
            } else {
                self.bottomView.hidden = YES;
                self.bottomViewConstraint.constant = 1;
            }
        }];
        
        [alertController addAction:action];
    }

    [self presentViewController:alertController animated:YES completion:nil];
}


/**
 选择发布合作范围点击事件
 */
- (IBAction)releaseLimitTapAction:(id)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    alertController.view.tintColor = [UIColor blackColor];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alertController addAction:cancelAction];
    
    NSArray *limitArr = @[@"全行业", @"公司", @"门店"];
    __weak typeof(self) weakSelf = self;
    for (NSInteger i = 0; i < limitArr.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:limitArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.cooperation_range = i + 1;
            weakSelf.releaseLimitTextField.text = limitArr[i];
            weakSelf.releaseLimitTextField.font = [UIFont systemFontOfSize:16];
        }];
        
        [alertController addAction:action];
    }

    [self presentViewController:alertController animated:YES completion:nil];
}

/**
 选择佣金百分比点击事件
 */
- (IBAction)partnerCommissionPercentClickAction:(id)sender {
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
    pickerView.title = @"佣金百分比";
    NSMutableArray *titleArray = [NSMutableArray arrayWithCapacity:100];
    for (NSInteger i = 0; i < 100; i++) {
        
        [titleArray addObject:[NSString stringWithFormat:@"%zd",i]];
    }
    pickerView.titleArray = titleArray;
    pickerView.isArea = NO;
    pickerView.firstIndex = self.commissionPercentIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        
        weakself.ownerComissonPercentTextField.textColor = COLOR(0x334A60);
        weakself.ownerComissonPercentTextField.font = [UIFont systemFontOfSize:16];
        weakself.ownerComissonPercentTextField.text = [NSString stringWithFormat:@"%@%@",firstTitle,@"%"];
        weakself.partnerCommissionPercentLabel.text =[NSString stringWithFormat:@"%zd%@",100 - [firstTitle integerValue],@"%"];
        weakself.commissionRate = [firstTitle integerValue];
        weakself.commissionPercentIndex = firstIndex;
        
    };
}

#pragma mark - 确定按钮点击事件
- (IBAction)confirmButtonClickAction:(UIButton *)sender {
    
    if (self.intentionRegionTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择意向区域" andTime:2 andColor:YES];
        return;
    }
    if (self.houseTypeTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择户型" andTime:2 andColor:YES];
        return;
    }
    if (self.minBudgetTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入最低预算" andTime:2 andColor:YES];
        return;
    }
    if (self.maxBudgetTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入最高预算" andTime:2 andColor:YES];
        return;
    }
    
    if (self.minAreaTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入最小面积" andTime:2 andColor:YES];
        return;
    }
    if (self.maxAreaTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入最大面积" andTime:2 andColor:YES];
        return;
    }
    if (self.buyerNameTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入买家姓名" andTime:2 andColor:YES];
        return;
    }
    if (self.buyerIDCardNoTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入买家身份证号码" andTime:2 andColor:YES];
        return;
    }
    if (![NSString checkUserIdCard:self.buyerIDCardNoTextField.text]) {
        [SISSTools showSVErrorTitle:@"您输入的身份证号码格式不正确" andTime:2 andColor:YES];
        return;
    }
    if (self.buyerPhoneNumTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入买家手机号码" andTime:2 andColor:YES];
        return;
    }
    
    if (![NSString checkTelNumber:self.buyerPhoneNumTextField.text]) {
        [SISSTools showSVErrorTitle:@"您输入的手机号码格式不正确" andTime:2 andColor:YES];
        return;
    }
    if (self.buyerNameTextField.text.length < 2) {
        [SISSTools showSVErrorTitle:@"姓名不能少于2个字" andTime:2 andColor:YES];
        return;
    }
    
    if (self.allowAgent == 1 && self.releaseLimitTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择合作发布范围" andTime:1.0 andColor:YES];
        return;
    };
    
    if (self.allowAgent == 1 && self.ownerComissonPercentTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择发布方佣金占比" andTime:1.0 andColor:YES];
        return;
    }
    SISSPassengerResourceModel *passengerModel = [[SISSPassengerResourceModel alloc] init];
    NSArray *regionArr = [self.intentionRegionTextField.text componentsSeparatedByString:@" "];
    passengerModel.region = regionArr[0];
    passengerModel.regionDetail = regionArr.count > 1 ? regionArr[1] : @"";
    passengerModel.neighbourhood = self.intentionCommunityTextField.text ? self.intentionCommunityTextField.text : @"";
    passengerModel.houseType = self.houseTypeTextField.text;
    passengerModel.minPrice = [self.minBudgetTextField.text floatValue];
    passengerModel.maxPrice = [self.maxBudgetTextField.text floatValue];
    passengerModel.minArea = [self.minAreaTextField.text floatValue];
    passengerModel.maxArea = [self.maxAreaTextField.text floatValue];
    if (passengerModel.minPrice > passengerModel.maxPrice) {
        [SISSTools showSVErrorTitle:@"最高预算不能小于最低预算字" andTime:2 andColor:YES];
        return;
    }
    if (passengerModel.minArea > passengerModel.maxArea) {
        [SISSTools showSVErrorTitle:@"最大面积不能小于最小面积" andTime:2 andColor:YES];
        return;
    }
    passengerModel.minFloor = self.minFloorTextField.text ? [self.minFloorTextField.text integerValue] : [@"" integerValue];
    passengerModel.maxFloor = self.maxFloorTextField.text ? [self.maxFloorTextField.text integerValue] : [@"" integerValue];
    if (passengerModel.minFloor > 0 && passengerModel.maxFloor > 0 && passengerModel.minFloor > passengerModel.maxFloor) {
        [SISSTools showSVErrorTitle:@"最高楼层不能小于最低楼层" andTime:2 andColor:YES];
        return;
    }
    passengerModel.decoration = self.decorationTextField.text ? self.decorationTextField.text : @"";
    passengerModel.direction = self.directionTextField.text ? self.directionTextField.text : @"";
    passengerModel.purpose = self.purchaseTextField.text ? self.purchaseTextField.text : @"";
    passengerModel.otherdesc = self.otherDescriptionTextView.text ? self.otherDescriptionTextView.text : @"";
    passengerModel.name = self.buyerNameTextField.text;
    passengerModel.idNumber = self.buyerIDCardNoTextField.text;
    passengerModel.phoneNumber = self.buyerPhoneNumTextField.text;
    passengerModel.insertTime = [NSDate yyr_dateCurrentTimeString];
    
    NSString *specialTagStr = [self.specialDemandTagsLabel.text isEqualToString:@"请选择特殊要求"] ? @"" : self.specialDemandTagsLabel.text;
    specialTagStr = [specialTagStr stringByReplacingOccurrencesOfString:@" " withString:@"#"];
    if ([specialTagStr hasSuffix:@"#"]) {
        specialTagStr = [specialTagStr substringToIndex:specialTagStr.length - 1];
    }
    passengerModel.tag = specialTagStr;
    
    if (self.passengerModel) {
        passengerModel.ID = self.passengerModel.ID;
        
        [self updatePassengerWithPassengerModel:passengerModel];
    } else {
        [self enrollPassengerWithPassengerModel:passengerModel];
    }
}





/**
 登记客源
 */
- (void)enrollPassengerWithPassengerModel:(SISSPassengerResourceModel *)passengerModel {
    
    [kJsonRequestNetworkingManager enrollPassengerResourceWithHouseModel:passengerModel successBlock:^(id responseObject) {
        
        NSLog(@"客源登记===>>>%@",responseObject);
       
        if ([responseObject[@"code"] integerValue] == 200) { // 登记成功
            if (self.allowAgent == 1) {
                NSString *cooperation_desc = self.cooperationDescriptionTextView.text;
                NSDictionary *parameters = @{
                                                    @"spId":[SISSUserAccount shared].spId,
                                                    @"soId":[SISSUserAccount shared].soId,
                                                    @"sobId":[SISSUserAccount shared].sobId,
                                                    @"abdId":responseObject[@"data"],
                                                    @"cooperation_type":@(2),
                                                    @"cooperation_range":@(self.cooperation_range),
                                                    @"commission_rate":@(self.commissionRate),
                                                    @"cooperation_desc":cooperation_desc
                                                    };
                       
                       [kNetworkManager releasePassengerCooperationWithparameters:parameters success:^(id responseObject) {
                           
                           NSLog(@"发布客源合作: %@",responseObject);
                           if ([responseObject[@"code"] integerValue] == 200) { // 发布成功
                               [self enrollSuccess:passengerModel abdId:responseObject[@"data"]];
                           }
                           
                       } failed:^(NSError *error) {
                           
                           NSLog(@"%@",error);
                       }];
            } else {
                [self enrollSuccess:passengerModel abdId:responseObject[@"data"]];
            }
            
            
        } else {// 登记失败
            if (responseObject[@"message"]) {
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [SISSTools showSVTitle:responseObject[@"message"] andTime:1.0 andColor:YES];
                }];
            } else {
                [SISSTools showSVErrorTitle:@"登记失败" andTime:2 andColor:YES];
            }
        }
        
    } failed:^(NSError *error) {
        NSLog(@"客源登记error: %@",error);
    }];
}

- (void)enrollSuccess:(SISSPassengerResourceModel*)passengerModel abdId:(NSString *)abdId {
    self.isConfirm = YES;
    [SISSTools showSVTitle:@"登记成功！" andTime:1.0 andColor:YES];
    if (self.isRegistration) {
        
        if (self.isCooperation) {
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[SISSPassengerResourceCooperationViewController class]]) {
                    [self.navigationController popToViewController:controller animated:YES];
                }
            }
            
        } else {
//            SISSEnrollPassengerDetailViewController *vc = [[SISSEnrollPassengerDetailViewController alloc] init];
//            vc.title = @"客源详情";
//            vc.isChange = NO;
//            vc.passengerModel = passengerModel;
//            vc.deleteComplete = ^{
//
//                [self dismissViewControllerAnimated:YES completion:nil];
//            };
            
            SISSPassengerRegistSuccessViewController *vc = [[SISSPassengerRegistSuccessViewController alloc] init];
            vc.abdId = abdId;
            vc.isRegistration = self.isRegistration;
            vc.isCooperation = self.isCooperation;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    } else {
        [self.navigationController popViewControllerAnimated:YES];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


/**
 修改客源
 */
- (void)updatePassengerWithPassengerModel:(SISSPassengerResourceModel *)passengerModel {
    
    [kJsonRequestNetworkingManager updatePassengerResourceWithPassengerModel:passengerModel successBlock:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) { // 修改成功
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [SISSTools showSVTitle:@"修改成功！" andTime:1.0 andColor:YES];
            }];
            
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[SISSMyEnrollPassengerViewController class]]) {
                    [self.navigationController popToViewController:controller animated:YES];
                }
            }
        }

    } failed:^(NSError *error) {
    
        NSLog(@"%@",error);
    }];
}

@end
